# encoding:utf-8
''''''
import os.path
import sys
import logging

def path_platcomp(path):
    return path.replace("/", os.sep)

def jit_encode(frompath, bittype="32bit", binpath=None):
    '''
    jitc文件夹
    @ismix 是不是32 64混合模式
    '''

    def run_cmd(outpath):
        '''执行jitc脚本'''
        cmd_str = "{jitbin} -b {filePath} {out_file}"

        #bin
        bin_str = "" 
        if binpath:
            os.chdir("{}/{}".format(binpath, bittype))
            bin_str = "./luajit-mac"
        else:
            if sys.platform == "win32":
                bin_str = "cd /d "+path_platcomp(os.path.dirname(__file__) + "/../bin/win32/lua/%s && ")% bittype + "luajit-win32" 
            else:
                os.chdir(path_platcomp(os.path.dirname(__file__) + "/../bin/mac/lua/%s")%bittype)
                bin_str = "./luajit-mac"
            
        cmd_str = cmd_str.format(jitbin=bin_str, filePath=outpath, out_file=outpath)
        os.system(cmd_str)
        logging.info("Compile the script->%s", outpath.split(os.sep)[-1])
    
    def iteral_path(path):
        '''遍历jitc脚本'''
        for filename in os.listdir(path):
            fileobj = os.path.join(path, filename)
            if os.path.isdir(fileobj):
                iteral_path(fileobj)
            else:
                if fileobj.endswith(".lua"):
                    run_cmd(fileobj)

    iteral_path(frompath)
